%{ nbPages = _caller.count.page(controllers.CRUD.getPageSize()) }%

<p class="crudCount">
    &{'crud.list.size', _caller.count, _caller.type.name}
</p>

<nav>
  
	#{if nbPages > 1}
	<ul class="pagination">
	        %{
	            limit = (_limit != null ? _limit : 10)
	            start = _caller.page - (int)(limit/2) + (limit % 2 ? 0 : 1)
	            end = _caller.page + (int)(limit/2)
	
	            if(start < 1){
	                extra = 1 - start
	                end = (end + extra < nbPages ? end + extra : nbPages)
	                start = 1
	            }else if(end > nbPages){
	                extra = end - nbPages
	                start = (start - extra > 1 ? start - extra : 1)
	                end = nbPages
	            }
	        }%
	
	        #{if _caller.page > 1}
	            <li>
	                #{a @list().add('page', _caller.page-1).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'crud.pagination.previous'}#{/a}
	            </li>
	        #{/if}
	
	        #{if start > 1}
	            <li >
	                #{a @list().add('page', 1).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'crud.pagination.first'}#{/a}
	            </li>
	        #{/if}
	
	        %{ for (i in start..end) { }%
	            
	                #{if _caller.page == i}
	                <li class="active">
	                    <span >${i}</span>
	                </li>
	                #{/if}
	                #{else}
	                <li>
	                    #{a @list().add('page', i).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}${i}#{/a}
	                </li>
	                #{/else}
	        %{ } }%
	
	        #{if end < nbPages}
	            <li >
	                #{a @list().add('page', nbPages).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'crud.pagination.last'}#{/a}
	            </li>
	        #{/if}
	
	        #{if _caller.page < nbPages}
	            <li >
	                #{a @list().add('page', _caller.page+1).add('search', _caller.params.search).add('orderBy', _caller.orderBy).add('order', _caller.order)}&{'crud.pagination.next'}#{/a}
	            </li>
	        #{/if}
	</ul>
	#{/if}
</nav>
